#include "uartHandle.h"
#include "uart.h"
#include "protocol.h"
#include "battery.h"
#include "check.h"
#include "rgb_led.h"
#include "led.h"
#include "vi5300.h"

// 模块包长
#define MODULE_LEN 6
// 模块个数
#define MODULE_TOTAL 37

static uint16_t rIndex = 0;
static uint16_t wIndex = 0;
static uint16_t r3Index = 0;
static uint16_t w3Index = 0;

static uint8_t controlDataBuf[MODULE_LEN] = {0};
static uint8_t sendPrevDataBuf[256] = {0xFD, 0xFD, MODULE_LEN*MODULE_TOTAL};
static uint8_t sendNextDataBuf[256] = {0xFD, 0xFD, MODULE_LEN*MODULE_TOTAL};

static uint8_t packageNum = 0;
static uint8_t beatHeart = 0;
static uint8_t checkNum = 0;

static void uartDataHandle(uint8_t data)
{
    static uint8_t status;
    static uint8_t cksum;
    static uint8_t len;
    static uint8_t bufIndex;
    static uint8_t buf[256];
    switch (status)
    {
    case 0:
        status = (data == 0xFD) ? 1 : 0;
        break;
    case 1:
        status = (data == 0xFD) ? 2 : 0;
        break;
    case 2:
        len = data;
        cksum = data;
        bufIndex = 0;
        status = 3;
        break;
    case 3:
        buf[bufIndex] = data;
        bufIndex++;
        cksum += data;
        if (bufIndex == len)
        {
            status = 4;
        }
        break;
    case 4:
        if (cksum == data) /*所有校验正确*/
        {
            memcpy(controlDataBuf, buf, MODULE_LEN);
            memcpy(sendNextDataBuf + 3, buf+MODULE_LEN, len - MODULE_LEN);
            // 第一次收到数据时直接传下一个设备
            send_next_data();
        }
        status = 0;
        break;
    default:
        break;
    }
}

static void uart3DataHandle(uint8_t data)
{
    static uint8_t status;
    static uint8_t cksum;
    static uint8_t len;
    static uint8_t bufIndex;
    static uint8_t buf[256];
    switch (status)
    {
    case 0:
        status = (data == 0xFD) ? 1 : 0;
        break;
    case 1:
        status = (data == 0xFD) ? 2 : 0;
        break;
    case 2:
        len = data;
        cksum = data;
        bufIndex = 0;
        status = 3;
        break;
    case 3:
        buf[bufIndex] = data;
        bufIndex++;
        cksum += data;
        if (bufIndex == len)
        {
            status = 4;
        }
        break;
    case 4:
        if (cksum == data) /*所有校验正确*/
        {
            memcpy(sendPrevDataBuf + 3 + MODULE_LEN, buf, len - MODULE_LEN);
        }
        status = 0;
        break;
    default:
        break;
    }
}

void uartUpdate(void)
{
    wIndex = getRemainRecvBuf();
    if (rIndex != wIndex)
    {
        int16_t len = wIndex - rIndex;
        if (len < 0)
        {
            len += 256;
        }

        uint16_t i = 0;
        while (i < len)
        {
            uint16_t index = rIndex + i;

            if (index > 255)
            {
                index -= 256;
            }
            uartDataHandle(g_rx[index]);
            i++;
        }
        rIndex = wIndex;
    }
    // uart3
    w3Index = getRemainRecv3Buf();
    if (r3Index != w3Index)
    {
        int16_t len = w3Index - r3Index;
        if (len < 0)
        {
            len += 256;
        }
        uint16_t i = 0;
        while (i < len)
        {
            uint16_t index = r3Index + i;

            if (index > 255)
            {
                index -= 256;
            }
            uart3DataHandle(g_rx3[index]);
            i++;
        }
        r3Index = w3Index;
    }
}

// 传给上一个
void send_prev_data(uint8_t tofFlag, uint8_t tofValue)
{
    static uint8_t noCheckFlag = 1;
    if (READ_CHECK)
    {
        if (!tofFlag)
        {
            LED_ON;
        }

        if (noCheckFlag)
        {
            checkNum++;
            noCheckFlag = 0;
        }
    }
    else
    {
        noCheckFlag = 1;
        if (!tofFlag)
        {
            LED_OFF;
        }
    }

    sendPrevDataBuf[3] = packageNum;
    sendPrevDataBuf[4] = getBatVol() * 10;

    sendPrevDataBuf[5] = tofValue;
    sendPrevDataBuf[6] = checkNum;
    sendPrevDataBuf[7] = beatHeart;
    sendPrevDataBuf[8] = sendPrevDataBuf[3] + sendPrevDataBuf[4] + sendPrevDataBuf[5] + sendPrevDataBuf[6] + sendPrevDataBuf[7];

    uint8_t sum = sendPrevDataBuf[2];
    uint8_t i;
    for (i = 0; i < MODULE_LEN*MODULE_TOTAL; i++)
    {
        sum += sendPrevDataBuf[3+i];
    }
    sendPrevDataBuf[3+i] = sum;
    uartSendData(sendPrevDataBuf, MODULE_LEN*MODULE_TOTAL+4);

    beatHeart++;
}

// 传给下一个设备
void send_next_data()
{
    uint8_t sum = sendNextDataBuf[2];
    uint8_t i;
    for (i = 0; i < MODULE_LEN*MODULE_TOTAL; i++)
    {
        sum += sendNextDataBuf[3+i];
    }
    sendNextDataBuf[3+i] = sum;
    uart3SendData(sendNextDataBuf, MODULE_LEN*MODULE_TOTAL+4);
}

void control_handle()
{
    if (controlDataBuf[0] != packageNum)
    {
        packageNum = controlDataBuf[0];
        switch (controlDataBuf[1])
        {
        case 1:
            rgb_led_set(controlDataBuf[2], controlDataBuf[3], controlDataBuf[4]);
            break;
        
        default:
            break;
        }
        
    }
}
